﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace PlayerStat
{
    public class FranceScraper : ScraperBase
    {
        private const DateTime startDate = new DateTime(2008, 8, 6);
        private const int StartYear = 2008;
        private const int MaxRound = 38;
        private const string MatchUrl = @"http://www.foot-national.com/{0}-{1}-calendrier-{2}-{3}.html";
        private const string MatchSite = @"http://www.foot-national.com/";

        private readonly int CurrentYear = DateTime.Now.Month >= 7 ? DateTime.Now.Year : DateTime.Now.Year - 1;

        protected override int[] LeagueIDs
        {
            get { throw new NotImplementedException(); }
        }

        public void Scrape()
        {
            foreach (int id in LeagueIDs)
            {

            }

            int eventYear = StartYear;

            while (eventYear <= CurrentYear)
            {
                int round = 1;

                while (round <= MaxRound)
                {
                    //this.Scrape(eventYear, round);
                    round += 1;
                }

                eventYear += 1;
            }
        }

        private void Scrape(int league, int year, int round)
        {
            string url = string.Format(MatchUrl, year, year + 1, league, round);

            HtmlDocument mainDoc = new HtmlDocument();

            mainDoc.Load("", Encoding.GetEncoding("ISO-8859-1"));


        }

        private DataTable GetMatchTable(HtmlDocument doc)
        {
            HtmlNode matchesNode = doc.DocumentNode.SelectSingleNode("//div[@id='calendrier']/table");

            if (matchesNode == null)
            {
                return null;
            }

            if ((matchesNode.InnerText.IndexOf("RÉSUMÉ") == -1 && matchesNode.InnerText.IndexOf("PARIEZ") == -1))
            {
                return null;
            }

            DataTable matchTable = new DataTable();

            HtmlNodeCollection rows = matchesNode.SelectNodes(".//tr");

            if (rows.Count == 0)
            {
                throw new Exception("No match date found");
            }

            DateTime matchDate = DateTime.MaxValue;

            foreach(HtmlNode row in rows)
            {
                //when the tag is "th", it's match date line
                if (row.FirstChild.Name == "th")
                {
                    string dateText = row.InnerText;

                    DateTime.TryParse(dateText, CultureInfo.GetCultureInfo("fr-Fr"), DateTimeStyles.None, out matchDate);

                    continue;
                }

                
                Entities.Match match = new Entities.Match();

                string timeText = row.SelectSingleNode("./td[@class='t0']").InnerText;

                DateTime time;
                DateTime.TryParse(timeText, CultureInfo.GetCultureInfo("fr-Fr"), DateTimeStyles.None, out time);

                DateTime matchTime = matchDate.AddHours(time.Hour);

                match.EventTime = matchTime;
                match.HomeTeam = row.SelectSingleNode("./td[@class='t1']").InnerText;
                
                string score = row.SelectSingleNode("./td[@class='t2']").InnerText;
                string[] homeAway = score.Split('-');
                match.HomeScore = int.Parse(homeAway[0].Trim());
                match.AwayScore = int.Parse(homeAway[1].Trim());

                match.AwayTeam = row.SelectSingleNode("./td[@class='t3']").InnerText;
                match.MatchStatUrl = row.SelectSingleNode("./td[@class='t4']/a").GetAttributeValue("href", string.Empty); 
            }

            //GetMatches(matchesNode, "");

            return null;
        }

        private void GetMatchStat(Entities.Match match)
        {
            //比赛技术统计

            
        }

        private void GetTeamStat()
        {

        }

        private DataTable GetMatches(HtmlNode node, string date)
        {
            throw new NotImplementedException();
        }
    }
}
